package gui.newgui.unassignedplayerspanel;

import gui.newgui.IArCompGuiComponent;
import gui.newgui.IController;
import gui.newgui.KeyListenerTypeSearch;
import gui.newgui.MouseListenerParticipantsTab;

import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import table.models.ParticipantsTableModel;
import table.models.ParticipantsTableModelRenderer;
import core.GuiComponentLabels;

public class UnassignedPlayersPanel extends javax.swing.JPanel implements IArCompGuiComponent {
	private final IController controller;
	private JTable playerTable;
	private JScrollPane tableScrollPane;
	private JLabel headerLabel;
	private KeyListener keyListenerTypeSearch;
	private MouseListener mouseListener;

	/**
	 * Auto-generated main method to display this JPanel inside a new JFrame.
	 */
	public static void main(String [] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new UnassignedPlayersPanel());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public UnassignedPlayersPanel() {
		super();
		this.controller = new UnassignedPlayersController(this);
		this.initGUI();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(this);
			this.setLayout(thisLayout);
			this.setPreferredSize(new Dimension(400, 300));
			{
				this.tableScrollPane = new JScrollPane();
				{
					this.keyListenerTypeSearch = new KeyListenerTypeSearch();
					this.mouseListener = new MouseListenerParticipantsTab();
					this.playerTable = new JTable();
					this.playerTable.addKeyListener(this.keyListenerTypeSearch);
					this.playerTable.addMouseListener(this.mouseListener);
					this.playerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
					this.playerTable.setModel(new ParticipantsTableModel(new String [] { GuiComponentLabels.ID, GuiComponentLabels.NAME, GuiComponentLabels.SURNAME, GuiComponentLabels.NATION,
							GuiComponentLabels.COUNTRY, GuiComponentLabels.BOW_CATHEGORY, GuiComponentLabels.AGE_CLASS, GuiComponentLabels.CLUB, GuiComponentLabels.GENDER, GuiComponentLabels.LICENSE,
							GuiComponentLabels.COUNTRY_RATING, GuiComponentLabels.BIRTHDATE }));
					this.playerTable.setAutoCreateRowSorter(true);
					this.playerTable.setDefaultRenderer(Object.class, new ParticipantsTableModelRenderer());
					this.playerTable.setDefaultRenderer(Integer.class, new ParticipantsTableModelRenderer());
					this.playerTable.setDefaultRenderer(Boolean.class, new ParticipantsTableModelRenderer());
					// TODO
					// this.playerTable.addKeyListener(this.keyListener);
					// this.playerTable.addMouseListener(this.mouseListener);
					this.controller.updateTable();

					this.tableScrollPane.setViewportView(this.playerTable);
				}
			}
			{
				this.headerLabel = new JLabel();
				this.headerLabel.setText(GuiComponentLabels.DB_ENTRIES);
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup().addContainerGap().addComponent(this.headerLabel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.tableScrollPane, 0, 238, Short.MAX_VALUE).addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout.createParallelGroup().addComponent(this.headerLabel, GroupLayout.Alignment.LEADING, 0, 376, Short.MAX_VALUE)
									.addComponent(this.tableScrollPane, GroupLayout.Alignment.LEADING, 0, 376, Short.MAX_VALUE)).addContainerGap());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public IController getController() {
		return this.controller;
	}

	public JTable getTable() {
		return this.playerTable;
	}

}
